package day0221;

public class CloneDemo {

    public static void main(String[] args) throws CloneNotSupportedException {
//        Object::clone 复制对象
        Outer outer = new Outer("李四");

        // 浅拷贝   深拷贝
        Outer clone = outer.getClone();
        System.out.println(outer);
        System.out.println(clone);
        System.out.println(clone.getName());

    }
}

class Outer implements Cloneable {
    private String name;


    public Outer(String name) {
        // 遮蔽: 同名的局部变量将成员变量遮蔽，在方法中使用的是局部变量
        this.name = name;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Outer getClone() throws CloneNotSupportedException {
        Object clone = this.clone();
        if (clone instanceof Outer outer) {
            return outer;
        }
        return null;
    }
}
